import config from "@/config";
import request from '../request';

/** 获取本地的用户信息*/
function getUserInfo() {
  return new Promise((resolve, reject) => {
    /** 通过config配置的用户信息存储key*/
    uni.sessionStorage.get(config.setStorageSyncField.userInfo).then(res => {
      if (res) {
        resolve(res);
      } else {
        reject(false)
      }
    }).catch(_ => {
      resolve(false);
    })
  });
}

function generateCode() {
  return request.ajax('/verification')
}

function loginByMobile(data) {
  return request.ajax('/loginByMobile', { method: 'post', ...data })
}

function userInfo() {
  return request.ajax('/info')
}

function sendSms(data) {
  return request.ajax('/sms', { method: 'post', ...data })
}

function register(data) {
  return request.ajax('/register', { method: 'post', ...data })
}

function loginByUserName(data) {
  return request.ajax('/login', { method: 'post', ...data })
}

const auth = {
  getUserInfo, generateCode, loginByMobile, userInfo, sendSms, register, loginByUserName
};
export default auth;
